﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;

using System.Text;

public partial class Admin_sms : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        
        string smsUID = null;
        string smsPWD = null;
        string smsSenderID = null;
        string smsAPIURLTemplate = "http://smsc.biz/httpapi/send?username={0}&password={1}&sender_id={2}&route=T&phonenumber={3}&message={4}";
        string customerMsg = "Dear {0}, Your AC Installation Req: {1} will be attended by {2}, Mob-{3}. For any query call 03323452562/63. T&C apply.";
        string installerMsg = "Custome: {0}, Contact: {1}, Address: {2}, PIN: {3}, Brand: {4}, Model: {5}.";
        string smsAPIURL = null;
        string smsResponse = null;
        string temp = null;
        WebClient client = new WebClient();

            smsUID = ConfigurationManager.AppSettings["SMS_USER_ID"].ToString() ?? null;
            smsPWD = ConfigurationManager.AppSettings["SMS_PWD"].ToString() ?? null;
            smsSenderID = ConfigurationManager.AppSettings["SMS_SENDER_ID"].ToString() ?? null;
            customerMsg = string.Format(customerMsg, "Somesh", "12345", "Nausad", "9313580352");
            installerMsg = string.Format(installerMsg, "Somesh Bhagat", "9871013553", "E-11/A 2nd Floor, Ganesh Nagar, Pandav Nagar Complex, Pandav Nagar, New Delhi", "110092", "Panasonic", "PAN-IND-62");

            temp = "Test sms from " + customerMsg + ". Thanks for choosing our service - Somesh - SMSC.BIZ";
            temp = HttpUtility.UrlEncode(temp);
            smsAPIURL = string.Format(smsAPIURLTemplate, smsUID, smsPWD, smsSenderID, "9871013553", temp);
            smsResponse = client.DownloadString(smsAPIURL);

            smsResponse = null;
            temp = "Test sms from " + installerMsg + ". Thanks for choosing our service - Somesh - SMSC.BIZ";
            temp = HttpUtility.UrlEncode(temp);
            smsAPIURL = string.Format(smsAPIURLTemplate, smsUID, smsPWD, smsSenderID, "9313580352", temp);            
            smsResponse = client.DownloadString(smsAPIURL);

        
       

        //WebClient client = new WebClient();
        //string baseurl = "http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=demouser&password=1303046044&sendername=ONLINE&mobileno=91" + txtToSend.Text + "&message=MySMSMantra-" + txtMessage.Text;
        //Stream data = client.OpenRead(baseurl);
        //StreamReader reader = new StreamReader(data);
        //string sResponse = reader.ReadToEnd();
        //Response.Write(sResponse);
        //data.Close();
        //reader.Close();




        //string sUserID = "somesh21jul";
        //string sPwd = "J2eenet!";
        //string sNumber = txtToSend.Text;
        //string sSID = "WEBSMS";
        //string sMessage = txtMessage.Text;
        //string sURL = "http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=" + sUserID + "&pwd=" + sPwd + "&to=" + sNumber + "&sid=" + sSID + "&msg=" + sMessage + "&fl=0&gwid=2";
        //string sResponse1 = GetResponse(sURL);
        //Response.Write(sResponse1);
    
    }


    public static string GetResponse(string sURL)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
        request.Method = "POST";       
        request.MaximumAutomaticRedirections = 4;
        request.Credentials = CredentialCache.DefaultCredentials;
        try
        {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
        Stream receiveStream = response.GetResponseStream ();
        StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
        string sResponse = readStream.ReadToEnd();
        response.Close ();
        readStream.Close ();
        return sResponse; 
        }
        catch(Exception ex)
        {
            return ex.Message;
        }
    }
}